import systools.Dialogs;
//import haxe.Serializer;

class App {

	static var flash : swhx.Flash;
	
	static function browseForFile() {
		//trace ("Function browseForFile() called in haxe backend");
		/*
		var filters: FILEFILTERS = 
			{ count: 2
			, descriptions: ["Text files", "JPEG files"]
			, extensions: ["*.txt","*.jpg;*.jpeg"]			
			};
		*/	
		
		var filters: FILEFILTERS = 
			{ count: 1
			, descriptions: ["All Files"]
			, extensions: ["*"]			
			};
		var result = Dialogs.openFile
			( "Select Sqlite DB File"
			, ""
			, filters 
			);
		
		return result;
	}
	
	static function browseForFolder(){
		var result = Dialogs.folder
			( "Select folder for generated classes"
			, ""			
			);
		
		return result;	
	}
	
	static function pathExists(path:String):Bool
	{
		return neko.FileSystem.exists(path);
	}
	
	/*
	 * Write a string to a file,overwrites existing file if it exists
	 */
	static function writeFile(filePath:String,fileContent:String):Bool
	{
		
		//fileContent = StringTools.baseDecode(fileContent,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
		
		var OK:Bool = true;
		
		if (neko.FileSystem.exists(filePath))
		{
			// attempt to delete it
			try
			{
				neko.FileSystem.deleteFile(filePath);
			}
			catch(e:Dynamic)
			{
				trace(e.toString());
				OK = false;
			}
		}
		
		
		var output = neko.io.File.write(filePath,false);
		
		try {
			output.write(fileContent);
			output.close();
		} 
		catch (e:Dynamic) {
			trace (e.toString());
			OK = false;
		}
		
		return OK;
	}
	
	
	
	static function getDBInfo(dbPath : String):Array<{name:String,sql:String}>
	{
		
		//trace("opening db file : " + dbPath);
		
		var cnx = neko.db.Sqlite.open(dbPath);
		
		var results:Array<{name:String,sql:String}> = new Array();
		
		var rset = cnx.request("SELECT name, sql FROM sqlite_master");
		
		for( row in rset ) {
			//trace(row.name);
			var sql:String = row.sql;
			// we are only interested in the sql to create tables, not indices etc...
			// check we actually have something in the sql field
			//trace (" SQL : " + sql);
			if (sql != null)
			{
				if (StringTools.startsWith(sql,"CREATE TABLE "))
				{
					results.push({name:row.name,sql:row.sql});
				}			
			}
			//else
			//{
				//trace(row.name + " sql was empty");	
			//}
		}
		
		return results;
	}
	
	
	static function main() {
		
		//editing/testing only, copy the flex swf to this folder ...
		
		//neko.io.File.copy("C:\\Documents and Settings\\Administrator\\My Documents\\Flex Builder 2\\spodGenFE\\bin\\spodGenFE.swf","spodGenFE.swf");
		//neko.io.File.copy("..\\spodgenFrontEnd\\bin\\spodGenFE.swf","spodGenFE.swf");
		
		swhx.Application.init(9);
		

		var width = 672;
		var height = 574;
				
		var server = new neko.net.RemotingServer();
		server.addObject("backend",App);

		var wnd = new swhx.Window("SpodGen Alpha",width,height);

		flash = new swhx.Flash(wnd,server);		
		flash.setAttribute("id","ui");
		flash.setAttribute("src","spodGenFE.swf");
		flash.start();
		wnd.resizable = true;		
		wnd.show(true);		
		
		wnd.onMinimize = function () { 
			trace("window is minimizing"); 
			return true; // return false to block minimize;
		};
		
		wnd.onMaximize = function () { 
			trace("window is maximizing"); 
			return true; // return false to block maximize;
		};
		
		wnd.onRightClick = function() {
			//trace("Supressing right-click menu by returning false on event");
			//return false;
			return true;
		}

		
		swhx.Application.loop();

		swhx.Application.cleanup();
	}

}
